/*图片上传*/
if(getQueryString("operateType")){
    var operateType = getQueryString("operateType");
}
var firstScenarioData = parent.firstScenarioData;
var loginData = JSON.parse(web.readCookie("loginData"));
var optionStr = '<option value="" selected>请选择一级场景</option>';
for (var i=0;i<firstScenarioData.length;i++){
    optionStr+='<option value="'+firstScenarioData[i].lable_id+'" >'+firstScenarioData[i].lable_name+'</option>';
}
$(".scene-level1").html(optionStr);
var dataInEditing = parent.dataInEditing;
var imgSrc = "";
var formData = new FormData();
function addBtn() {
    $("#file").trigger('click');
}
function putDataToPage(e) {
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function (e) {
            getImageInfo(e.target.result,function (width,height) {
                if(width==1200&&height==420){
                    formData.set('file', $("#file")[0].files[0]);
                    uploadImg();
                }else {
                    layer.msg("请上传正确尺寸",{icon: 5,time:1000},function () {});
                }
            })
        }
    }
}

function uploadImg(){
    $.ajax({
        url: root.inter.imgUpload,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData : false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType : false,
        data: formData,
        success: function (data) {
            if(data&&data != null&&data!=""){
                imgSrc = data[0].fullPath;
                $("#myImg").attr("src", imgSrc);
            }else{
                layer.msg(data.errorMessage, {icon: 5,time: 1000});
            }
        }
    });
}
/*表单校验查询*/
$("#form-sceneAdd").Validform({
    tiptype: 2,
    callback: function () {
        saveDataFn();
        return false;
    }
});
function saveDataFn() {
    var params = {
        "id":"",  //--修改时必传
        "sceneId":$(".scene-level1").val(),
        "sceneTitle":$("#sceneTitle").val(),
        "sceneImgUrl":imgSrc,
        "sceneIndex":$("#sceneIndex").val(),
        "bottomColor":$("#bottomColor").val(),
        "effectTime":$("#effectTime").val(),
        "expiredTime":$("#expiredTime").val()
    }
    if(operateType=="add"){
        params.creatorId = loginData.user.user_ID;
        params.creatorName = loginData.user.name;
        params.id = "";
    }else if(operateType=="edit"){
        params.modifyId =loginData.user.user_ID;
        params.modifyName = loginData.user.name;
        params.id = dataInEditing.id;
    }
    var judgmentFieldObj = {
        "effectTime":"生效时间不能为空",
        "bottomColor":"底层色值不能为空",
        "sceneIndex":"场景顺序不能为空",
        "sceneImgUrl":"图片不能为空",
        "sceneTitle":"标题不能为空",
        "sceneId":"场景不能为空",
    }
    var isContinue = nullValueJudgmentFn(params,judgmentFieldObj);
    if(!isContinue){
        return;
    }
    var succCallBack = function(data, status, response) {
        if(data.errorCode == "0") {
            layer.msg("操作成功", {
                icon: 6,
                time: 1000
            }, function() {
                parent.getScenarioData(1);
                closePop();
            })
        } else {
            layer.msg(data.msg, {
                icon: 5,
                time: 1000
            }, function() {

            })
        }
    };
    return web.doAjax(root.scenario.appScene_saveOrUpdate, 'post', params, succCallBack,1);
}

// 数据渲染
if(operateType=="edit"){
    renderData();
}
function renderData(){
    imgSrc = dataInEditing.sceneImgUrl;
    $("#myImg").attr("src", imgSrc);
    $(".scene-level1").val(dataInEditing.sceneId);
    $("#sceneTitle").val(dataInEditing.sceneTitle);
    $("#sceneIndex").val(dataInEditing.sceneIndex);
    $("#bottomColor").val(dataInEditing.bottomColor);
    $("#effectTime").val(dealNull(dataInEditing.effectTime));
    $("#expiredTime").val(dealNull(dataInEditing.expiredTime));
}

// 时间初始化
timeSet("#effectTime","#expiredTime","YYYY-MM-DD hh:mm:ss");
/*selectTime();
function selectTime(){
    var startTime = {
        format: 'YYYY-MM-DD hh:mm:ss',
        clearfun:function(val) {
            start.maxDate ="2099-12-31 23:59:59";
            end.minDate="1900-01-01 00:00:00";
            end.maxDate ="2099-12-31 23:59:59";
        }
    };
    var endTime = {
        format: 'YYYY-MM-DD hh:mm:ss', clearfun:function(val) {
            start.maxDate ="2099-12-31 23:59:59";
            end.maxDate ="2099-12-31 23:59:59";
        },
    };
    $.jeDate("#effectTime",startTime);
    $.jeDate("#expiredTime",endTime);
}*/
/*关闭弹框并刷新列表*/
function closePop() {
    parent.layer.closeAll();
}